package com.tust.app.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tust.app.domain.Topic;
import com.tust.app.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class TopicController {

    @Autowired
    private TopicService topicService;

    @RequestMapping("/tpage")
    public String tpage(HttpServletRequest request) {
        PageHelper.startPage(1, 3);
        List<Topic> topic = topicService.topicList();
        PageInfo page = new PageInfo(topic);
        request.setAttribute("pageInfo", page);
        return "home";
    }

    @RequestMapping("/tpage/{pageNum}")
    public String tpage2(@PathVariable("pageNum") Integer pageNum, HttpServletRequest request){
        PageHelper.startPage(pageNum, 3);
        List<Topic> topic = topicService.topicList();
        PageInfo page = new PageInfo(topic);
        request.setAttribute("pageInfo", page);
        return "home";
    }

    @RequestMapping("/atopic")
    public String listAdminTopics(Integer pn, HttpServletRequest request) {
        if (pn == null) {
            pn = 1;
        }
        PageHelper.startPage(pn, 3);
        List<Topic> topic = topicService.topicList();
        PageInfo page = new PageInfo(topic);
        request.setAttribute("topicList", page); // Ensure attribute name matches JSP
        return "admin/topic/topicList";
    }

    @RequestMapping(value = "/writeTopic")
    public String writeTopic(String title, String detail, HttpServletRequest request) {

        // 构造一个 Topic 对象
        Topic topic = new Topic();
        topic.setTitle(title);
        topic.setDetail(detail);

        // 设置发布时间为当前时间
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String pubtime = format.format(date);
        topic.setPubtime(pubtime);

        // 设置编辑用户和状态
        String edituser = (String) request.getSession().getAttribute("username");
        if (edituser == null) {
            edituser = "defaultUser"; // 默认用户，可以根据实际情况修改
        }
        topic.setEdituser(edituser);
        topic.setState(1); // 假设状态为1表示已发布，可以根据实际情况修改

        // 添加文章
        topicService.insert(topic);

        // 查询所有的文章，传递给页面
        PageHelper.startPage(1, 10);
        List<Topic> topics = topicService.findAll();
        PageInfo<Topic> pageInfo = new PageInfo<>(topics);
        request.setAttribute("topicList", pageInfo);
        return "admin/topic/topicList";
    }

    @RequestMapping("/selectByTitlet")
    public String selectByTitlet(String title, HttpServletRequest request){
        //根据title去查询相关的文章
        PageHelper.startPage(1, 8);
        List<Topic> searchList = topicService.search(title);
        PageInfo pageInfo = new PageInfo(searchList);
        request.setAttribute("topicList", pageInfo);
        if (searchList != null){
            return "admin/topic/topicList";
        }
        return "error";
    }

    @RequestMapping("/getTopic")
    public String getTopic(Integer id, HttpServletRequest request){
        //根据id获取对应新闻的信息
        Topic topic = topicService.findTopicById(id);
        //把查询出来的news对象传递到页面上去
        request.setAttribute("topic", topic);
        return "admin/topic/edittopic";
    }

    @RequestMapping("/updateTopic")
    public String updateNews(Integer id, String title, String detail){
        //实现修改的过程
        Topic topic = new Topic();
        topic.setId(id);
        topic.setTitle(title);
        topic.setDetail(detail);
        boolean b = topicService.modifyTopic(topic);
        if (b){
            return "forward:/atopic";
        }
        return "error";
    }

    @RequestMapping("/deleteTopicById")
    public String deleteTopicByI(Integer id){
        //完成删除的功能
        boolean b = topicService.delTopic(id);
        if (b){
            return "forward:/atopic";
        }
        return "error";
    }

    @RequestMapping("/selectTopicById")
    public String selectTopicById(Integer id, HttpServletRequest request){
        //根据id查询一条新闻消息
        Topic topic = topicService.findTopicById(id);
        request.setAttribute("topic", topic);
        return "detail";
    }


}